{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "af709f2f-9c40-4f61-be6f-85e4f8d64ae0",
   "metadata": {},
   "source": [
    "# 第二章 PyTorch与数学基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "803e97f9-0c9e-4f37-8721-3f670f94dc9b",
   "metadata": {},
   "source": [
    "## 2.1　PyTorch中的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b66602e1-2332-42d3-9393-0c4e6693f96a",
   "metadata": {},
   "source": [
    "### 2.1.2　PyTorch中的主要函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0db06a3d-a3f9-4f2d-b99b-347c4aed53ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c70a47e8-7410-4c4a-8861-8f8951299102",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3027047487569900"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.seed()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9901669b-0ab9-4ce8-8b1e-f8cb81aa00f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<torch._C.Generator at 0x26fa51382d0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.manual_seed(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ad1b2cc3-f074-48f3-99cf-e8646f4982c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.initial_seed()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "26f222b0-1bf7-4ce4-8aca-d9f8873260c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 0, 0,  ..., 0, 0, 0], dtype=torch.uint8)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.get_rng_state()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "be806c13-ac9d-45f7-8963-7306d36b712a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1, 0, 0,  ..., 0, 0, 0], dtype=torch.uint8)\n",
      "tensor([2, 0, 0,  ..., 0, 0, 0], dtype=torch.uint8)\n"
     ]
    }
   ],
   "source": [
    "rng_state1=torch.get_rng_state()\n",
    "print(rng_state1)\n",
    "torch.set_rng_state(rng_state1*2)\n",
    "rng_state2=torch.get_rng_state()\n",
    "print(rng_state2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "870faab6-770a-405f-9a7a-bbab67c43b70",
   "metadata": {},
   "source": [
    "## 2.2　微分基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8320a8f6-1b74-4110-8254-209938aa0199",
   "metadata": {},
   "source": [
    "### 2.2.2　PyTorch自动微分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3ae75946-decf-4f40-91da-4cafd3a5d91f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([5.])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "w=torch.tensor([1.],requires_grad=True)\n",
    "x=torch.tensor([2.],requires_grad=True)\n",
    "a=torch.add(x,w)\n",
    "b=torch.add(w,1)\n",
    "y=torch.mul(a,b)\n",
    "y.backward()\n",
    "print(w.grad)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2f863d5-8414-48a3-adf0-6e4d3ea77985",
   "metadata": {},
   "source": [
    "### grad_tensors参数的用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e3e8e168-da78-44cf-9568-8f16b3ce1cd2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([9.])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "w=torch.tensor([1.],requires_grad=True)\n",
    "x=torch.tensor([2.],requires_grad=True)\n",
    "a=torch.add(x,w)\n",
    "b=torch.add(w,1)\n",
    "y0=torch.mul(a,b)\n",
    "y1=torch.add(a,b)\n",
    "loss=torch.cat([y0,y1],dim=0)\n",
    "grad_t=torch.tensor([1.,2.])\n",
    "loss.backward(gradient=grad_t)\n",
    "print(w.grad)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74c6a170-b53d-4fc4-8156-a830015d25eb",
   "metadata": {},
   "source": [
    "### torch.autograd.grad()该函数实现求取梯度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dbb69be6-45e6-4636-a0f7-d0ea4992015a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(tensor([6.], grad_fn=<MulBackward0>),)\n",
      "(tensor([2.]),)\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "x=torch.tensor([3.],requires_grad=True)\n",
    "y=torch.pow(x,2)\n",
    "grad1=torch.autograd.grad(y,x,create_graph=True)\n",
    "print(grad1)\n",
    "grad2=torch.autograd.grad(grad1[0],x)\n",
    "print(grad2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bb9c8fb-cf90-4b7c-bc83-c88e46fe4071",
   "metadata": {},
   "source": [
    "### 梯度不能自动清零，在每次反向传播中会叠加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ad0ebcfe-c6d3-4c98-bc12-372dc42ace3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([5.])\n",
      "tensor([10.])\n",
      "tensor([15.])\n"
     ]
    }
   ],
   "source": [
    "w=torch.tensor([1.],requires_grad=True)\n",
    "x=torch.tensor([2.],requires_grad=True)\n",
    "for i in range(3):\n",
    "    a=torch.add(x,w)\n",
    "    b=torch.add(w,1)\n",
    "    y=torch.mul(a,b)\n",
    "    y.backward()\n",
    "    print(w.grad)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "123ffdd4-9959-4187-8531-9a8c6e17a533",
   "metadata": {},
   "source": [
    "### 这会导致我们得不到正确的结果，所以需要手动清零，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "94215ee3-86af-4bf6-a025-8557598ea3b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([5.])\n",
      "tensor([5.])\n",
      "tensor([5.])\n"
     ]
    }
   ],
   "source": [
    "w=torch.tensor([1.],requires_grad=True)\n",
    "x=torch.tensor([2.],requires_grad=True)\n",
    "for i in range(3):\n",
    "    a=torch.add(x,w)\n",
    "    b=torch.add(w,1)\n",
    "    y=torch.mul(a,b)\n",
    "    y.backward()\n",
    "    print(w.grad)\n",
    "    w.grad.zero_() # 梯度清零"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59d7ff7d-af3f-41d3-b34c-5c5e7f466af3",
   "metadata": {},
   "source": [
    "### 依赖于叶子节点的节点，requires_grad默认为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5e8626b7-9383-40a5-95bd-c8083479797c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True True True\n"
     ]
    }
   ],
   "source": [
    "w=torch.tensor([1.],requires_grad=True)\n",
    "x=torch.tensor([2.],requires_grad=True)\n",
    "a=torch.add(x,w)\n",
    "b=torch.add(w,1)\n",
    "y=torch.mul(a,b)\n",
    "print(a.requires_grad,b.requires_grad,y.requires_grad)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f1c46fa-8b14-43b1-a843-27a9178988d1",
   "metadata": {},
   "source": [
    "#### 叶子节点不可以执行in-place，因为前向传播记录了叶子节点的地址，反向传播需要用到叶子节点的数据时，要根据地址寻找数据，执行in-place操作改变了地址中的数据，梯度求解也会发生错误，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a52b14ba-8f28-48e4-96d7-277081fd2a13",
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "a leaf Variable that requires grad is being used in an in-place operation.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 6\u001b[0m\n\u001b[0;32m      4\u001b[0m b\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39madd(w,\u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m      5\u001b[0m y\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mmul(a,b)\n\u001b[1;32m----> 6\u001b[0m \u001b[43mw\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: a leaf Variable that requires grad is being used in an in-place operation."
     ]
    }
   ],
   "source": [
    "w=torch.tensor([1.],requires_grad=True)\n",
    "x=torch.tensor([2.],requires_grad=True)\n",
    "a=torch.add(x,w)\n",
    "b=torch.add(w,1)\n",
    "y=torch.mul(a,b)\n",
    "w.add_(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6c27f12-0932-4688-90c7-cc033857d19a",
   "metadata": {},
   "source": [
    "#### in-place操作即原位操作，在原始内存中改变这个数据，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e967b03b-d6ab-44bd-80d1-97cecda3e9fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2170818339264 tensor([1])\n"
     ]
    }
   ],
   "source": [
    "a=torch.tensor([1])\n",
    "print(id(a),a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "621539b6-ecdd-4cad-83fa-2f62536145b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2170823971440 tensor([2])\n"
     ]
    }
   ],
   "source": [
    "#开辟新的内存地址\n",
    "a=a+torch.tensor([1])\n",
    "print(id(a),a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c22e3512-92e8-442b-8920-8f53eba8807e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2170823971440 tensor([3])\n"
     ]
    }
   ],
   "source": [
    "#in-place操作，地址不变\n",
    "a+=torch.tensor([1])\n",
    "print(id(a),a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27318aea-cb57-4510-963a-eb8cf789f0bc",
   "metadata": {},
   "source": [
    "## 2.3　数理统计基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c77d543-93f8-408f-8167-7bd378276aab",
   "metadata": {},
   "source": [
    "### 2.3.2　PyTorch统计函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fb33287-9346-4cbb-99bf-c3acd79aa9ea",
   "metadata": {},
   "source": [
    "#### torch.prob()函数返回所有元素的积，用法如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1a1468a1-4233-453e-96fc-c70f582c7d0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(3)\n"
     ]
    }
   ],
   "source": [
    "print(torch.prod(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d2590c9-1882-4ccb-a205-420b428003e5",
   "metadata": {},
   "source": [
    "#### torch.sum()函数对输入的tensor数据的某一维度求和，一共有两种用法，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3557214d-663b-43dc-8005-14e3bf44ec46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.1586, 0.3247],\n",
      "        [0.2825, 0.9181]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "a=torch.rand(2,2)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8136478b-5ce3-438f-aa5f-378173696669",
   "metadata": {},
   "source": [
    "#### 设置参数input和dim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2940e9fb-a399-4566-a23c-596d10f360d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(1.6838)\n",
      "tensor(1.6838)\n",
      "tensor([0.4411, 1.2427])\n",
      "tensor([0.4832, 1.2005])\n"
     ]
    }
   ],
   "source": [
    "a1=torch.sum(a)\n",
    "a2=torch.sum(a,dim=(0,1))\n",
    "a3=torch.sum(a,dim=0)\n",
    "a4=torch.sum(a,dim=1)\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4f266c5-aa7c-4f10-b0c4-90c7b7224e8a",
   "metadata": {},
   "source": [
    "#### 设置参数keepdim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1a4694b5-e022-4437-9862-5f8f5eed3afd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.6838]])\n",
      "tensor([[0.4411, 1.2427]])\n",
      "tensor([[0.4832],\n",
      "        [1.2005]])\n"
     ]
    }
   ],
   "source": [
    "a5=torch.sum(a,dim=(0,1),keepdim=True)\n",
    "a6=torch.sum(a,dim=(0,),keepdim=True)\n",
    "a7=torch.sum(a,dim=(1,),keepdim=True)\n",
    "print(a5)\n",
    "print(a6)\n",
    "print(a7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1b8e5b2-0fc6-40be-b628-4e7b8b154de0",
   "metadata": {},
   "source": [
    "#### 3．平均值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "881ce3c3-d753-4624-8cf5-52018a1ce63f",
   "metadata": {},
   "source": [
    "#### torch.mean()函数对输入的tensor数据的某一维度求平均值，参数与torch.sum()函数类似，也有两种用法："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2cdefd2-ee84-4841-ae03-d93fc04a90dd",
   "metadata": {},
   "source": [
    "#### 设置参数input和dim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b625a635-2ada-4bf5-83b0-cff3c22562c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.4209)\n",
      "tensor(0.4209)\n",
      "tensor([0.2205, 0.6214])\n",
      "tensor([0.2416, 0.6003])\n"
     ]
    }
   ],
   "source": [
    "a8=torch.mean(a)\n",
    "a9=torch.mean(a,dim=(0,1))\n",
    "a10=torch.mean(a,dim=0)\n",
    "a11=torch.mean(a,dim=1)\n",
    "print(a8)\n",
    "print(a9)\n",
    "print(a10)\n",
    "print(a11)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f3b5e3a-aa5f-4e9d-930a-51fde43d3821",
   "metadata": {},
   "source": [
    "#### 设置参数keepdim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a0a9c35f-45e4-471c-b707-597da3a9c8e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.4209]])\n",
      "tensor([[0.2205, 0.6214]])\n",
      "tensor([[0.2416],\n",
      "        [0.6003]])\n"
     ]
    }
   ],
   "source": [
    "a12=torch.mean(a,dim=(0,1),keepdim=True)\n",
    "a13=torch.mean(a,dim=(0,),keepdim=True)\n",
    "a14=torch.mean(a,dim=(1,),keepdim=True)\n",
    "print(a12)\n",
    "print(a13)\n",
    "print(a14)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fccd3bf-e01e-4280-a241-f8b686d5469e",
   "metadata": {},
   "source": [
    "### 4．最大值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bbe13d1-7b42-4641-be7d-a8b3d4e7b52b",
   "metadata": {},
   "source": [
    "#### torch.max()函数返回最大值，参数与torch.sum()函数类似，但是参数dim需要是整数，也有两种用法，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cae68a09-7103-4365-8b14-4e76142b5782",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.9181)\n",
      "torch.return_types.max(\n",
      "values=tensor([0.2825, 0.9181]),\n",
      "indices=tensor([1, 1]))\n",
      "torch.return_types.max(\n",
      "values=tensor([0.3247, 0.9181]),\n",
      "indices=tensor([1, 1]))\n"
     ]
    }
   ],
   "source": [
    "a15=torch.max(a)\n",
    "a16=torch.max(a,dim=0)\n",
    "a17=torch.max(a,dim=1)\n",
    "print(a15)\n",
    "print(a16)\n",
    "print(a17)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dbecd7e-568c-4c49-8e8c-305ee7ea64ed",
   "metadata": {},
   "source": [
    "#### 设置参数keepdim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f55fd5cd-1e54-4d32-85a4-814289a06ec4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.return_types.max(\n",
      "values=tensor([[0.2825, 0.9181]]),\n",
      "indices=tensor([[1, 1]]))\n",
      "torch.return_types.max(\n",
      "values=tensor([[0.3247],\n",
      "        [0.9181]]),\n",
      "indices=tensor([[1],\n",
      "        [1]]))\n"
     ]
    }
   ],
   "source": [
    "a18=torch.max(a,0,keepdim=True)\n",
    "a19=torch.max(a,1,keepdim=True)\n",
    "print(a18)\n",
    "print(a19)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fa3435e-eb27-491a-b0c9-b1644030aa1a",
   "metadata": {},
   "source": [
    "### 5．最小值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70681acb-f7a4-4a44-8298-47661f2d1bef",
   "metadata": {},
   "source": [
    "#### torch.min()函数返回最小值，参数与torch.max()函数类似，也有两种用法，代码如下："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3624830e-8b7f-444f-9261-741a05532e0e",
   "metadata": {},
   "source": [
    "#### 设置参数input和dim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7b8af0af-9718-4c29-b89b-6880e40a05dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.1586)\n",
      "torch.return_types.min(\n",
      "values=tensor([0.1586, 0.3247]),\n",
      "indices=tensor([0, 0]))\n",
      "torch.return_types.min(\n",
      "values=tensor([0.1586, 0.2825]),\n",
      "indices=tensor([0, 0]))\n"
     ]
    }
   ],
   "source": [
    "a20=torch.min(a)\n",
    "a21=torch.min(a,dim=0)\n",
    "a22=torch.min(a,dim=1)\n",
    "print(a20)\n",
    "print(a21)\n",
    "print(a22)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4a3ce64-c8ed-4fd9-8d26-8f62e842084e",
   "metadata": {},
   "source": [
    "#### 设置参数keepdim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "71a8792e-dcd8-4480-8e35-82d51eb8eae4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.return_types.min(\n",
      "values=tensor([[0.1586, 0.3247]]),\n",
      "indices=tensor([[0, 0]]))\n",
      "torch.return_types.min(\n",
      "values=tensor([[0.1586],\n",
      "        [0.2825]]),\n",
      "indices=tensor([[0],\n",
      "        [0]]))\n"
     ]
    }
   ],
   "source": [
    "a23=torch.min(a,0,keepdim=True)\n",
    "a24=torch.min(a,1,keepdim=True)\n",
    "print(a23)\n",
    "print(a24)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "507de031-2468-4fa0-8093-b45a272f1289",
   "metadata": {},
   "source": [
    "### 6．中位数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acb7198b-7a1d-43e2-9abf-faa24ecafe90",
   "metadata": {},
   "source": [
    "#### torch.median()：返回中位数，参数与torch.max()函数类似，也有两种用法，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "db2f7e71-5859-431d-9729-ebafb6dd51b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.2825)\n",
      "torch.return_types.median(\n",
      "values=tensor([0.1586, 0.2825]),\n",
      "indices=tensor([0, 0]))\n"
     ]
    }
   ],
   "source": [
    "print(torch.median(a))\n",
    "print(torch.median(a,1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b017149-65d8-4376-aa01-c0b8c02f83ea",
   "metadata": {},
   "source": [
    "#### 设置参数keepdim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "38f73712-665e-41f9-a456-4e1033f9fbf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.return_types.median(\n",
       "values=tensor([[0.1586],\n",
       "        [0.2825]]),\n",
       "indices=tensor([[0],\n",
       "        [0]]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.median(a,1,keepdim=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9363536-4864-4bb9-9e4f-fad75c2bb1ea",
   "metadata": {},
   "source": [
    "### 7．众数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4138def-1a7b-4c37-bbe9-37052adbfee1",
   "metadata": {},
   "source": [
    "#### torch.mode()：返回众数，参数与torch.max()函数类似，也有两种用法，代码如下："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4446916-6c0e-4eab-b9b5-ca4b66c5b28b",
   "metadata": {},
   "source": [
    "#### 设置参数input和dim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a3daf6a0-aa6f-4422-a9b8-1157a15721b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.return_types.mode(\n",
      "values=tensor([0.1586, 0.2825]),\n",
      "indices=tensor([0, 0]))\n",
      "torch.return_types.mode(\n",
      "values=tensor([0.1586, 0.3247]),\n",
      "indices=tensor([0, 0]))\n"
     ]
    }
   ],
   "source": [
    "print(torch.mode(a))\n",
    "print(torch.mode(a,0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "047eb2ad-1afa-45d8-9435-088d97434104",
   "metadata": {},
   "source": [
    "#### 设置参数keepdim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "19094589-c154-4fb0-80da-ad597b05605f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.return_types.mode(\n",
       "values=tensor([[0.1586],\n",
       "        [0.2825]]),\n",
       "indices=tensor([[0],\n",
       "        [0]]))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mode(a,1,keepdim=True) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f4003ed-88f3-42bb-8760-4dcbe2444274",
   "metadata": {},
   "source": [
    "### 8．方差"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "588287d5-b028-4d17-9819-8b4fc2bd5563",
   "metadata": {},
   "source": [
    "#### torch.var()：返回输入张量中所有元素的方差，也有两种用法，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bcc42ccf-e80d-4b2b-bcd6-e2fdf8a9ff38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.0138, 0.2020])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.var(a,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "906815cd-8a90-45e8-a81a-ea296b57ae5c",
   "metadata": {},
   "source": [
    "#### 设置参数unbiased，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7e6d1f36-ffb2-484b-b8b8-4d6faa191c86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.0069, 0.1010])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.var(a,1,unbiased=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e7e02bd-f8ae-4fde-9ece-c021481639e2",
   "metadata": {},
   "source": [
    "#### 设置参数keepdim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7d5477e0-7d42-4cf8-bbca-233a1fd5bbe4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.0069],\n",
       "        [0.1010]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.var(a,1,unbiased=False,keepdim=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97ac4398-d70a-447a-8cf0-f9b7ab3ff54b",
   "metadata": {},
   "source": [
    "### 9．标准差"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b7458d0-2c28-404a-9012-c445f874364f",
   "metadata": {},
   "source": [
    "#### torch.std()：返回输入张量中所有元素的标准差，参数与torch.var()函数类似，也有两种用法，代码如下："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a87a575-4ae5-4d52-a99e-27110ccf44cd",
   "metadata": {},
   "source": [
    "#### 设置参数input和dim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e6d4ddfc-2e66-406b-9349-9b905fdf2276",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.1174, 0.4494])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.std(a,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0aa4290-d564-4d62-9c5d-bd5598673cad",
   "metadata": {},
   "source": [
    "#### 设置参数unbiased，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "fefdd3c5-516f-4a87-a41b-53a9a354520b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.0830, 0.3178])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.std(a,1,unbiased=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "332c7678-26e9-48e4-b901-d78909b86275",
   "metadata": {},
   "source": [
    "#### 设置参数keepdim，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "27c2b47c-6d76-4a83-a281-e28441526fc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.0830],\n",
       "        [0.3178]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.std(a,1,unbiased=False,keepdim=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d877d463-c86f-4acf-817e-d46ae5cc16ff",
   "metadata": {},
   "source": [
    "## 2.4　矩阵基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c35d0bbe-4994-485f-824c-782ef1a9063c",
   "metadata": {},
   "source": [
    "### 2.4.2　PyTorch矩阵运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41f04fc5-db81-475f-b549-542ed917f46f",
   "metadata": {},
   "source": [
    "#### 1．矩阵的加法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc14cf18-94a2-4a9b-8654-9cd053e61f41",
   "metadata": {},
   "source": [
    "#### 首先创建两个张量，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "cb21098a-b3ba-4b4e-a12d-d2486ede9fc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.9280, 0.0326, 0.6532, 0.8155],\n",
      "        [0.3355, 0.0518, 0.0210, 0.3272],\n",
      "        [0.8282, 0.0620, 0.0604, 0.9638]])\n",
      "tensor([0.4250, 0.0120, 0.0721, 0.7464])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "a=torch.rand(3,4)\n",
    "b=torch.rand(4)\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47012e21-f6fc-48d3-ab00-92c8d32d709b",
   "metadata": {},
   "source": [
    "### 方法1：使用+运算符实现加法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "21e8b78f-4cb1-4c7f-b4b2-7b121e2ad377",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.3530, 0.0446, 0.7252, 1.5619],\n",
      "        [0.7605, 0.0638, 0.0931, 1.0736],\n",
      "        [1.2532, 0.0740, 0.1325, 1.7102]])\n"
     ]
    }
   ],
   "source": [
    "print(a+b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4192d1b2-8147-480a-a2ae-1b4b3e5301d6",
   "metadata": {},
   "source": [
    "### 方法2：使用函数torch.add()实现加法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c7695804-8e77-464b-848d-c8f47072fc8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.3530, 0.0446, 0.7252, 1.5619],\n",
      "        [0.7605, 0.0638, 0.0931, 1.0736],\n",
      "        [1.2532, 0.0740, 0.1325, 1.7102]])\n"
     ]
    }
   ],
   "source": [
    "print(torch.add(a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "057d0c1d-f3e2-444c-968d-d9a489618d13",
   "metadata": {},
   "source": [
    "### 方法3：输出到一个向量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d6287c38-bb49-4cb3-9401-391fa055fccb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.3530, 0.0446, 0.7252, 1.5619],\n",
      "        [0.7605, 0.0638, 0.0931, 1.0736],\n",
      "        [1.2532, 0.0740, 0.1325, 1.7102]])\n"
     ]
    }
   ],
   "source": [
    "c=torch.Tensor(3,4)\n",
    "print(torch.add(a,b,out=c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6402dba-d1ca-4570-96c7-c950ea62ec31",
   "metadata": {},
   "source": [
    "### 方法4：把一个张量加到另一个张量上。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "0f2e6c38-72f1-46be-83d1-96506b6b348d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.3530, 0.0446, 0.7252, 1.5619],\n",
      "        [0.7605, 0.0638, 0.0931, 1.0736],\n",
      "        [1.2532, 0.0740, 0.1325, 1.7102]])\n"
     ]
    }
   ],
   "source": [
    "print(b.add(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36ee861a-264b-4d96-8498-98ee0e1bef8b",
   "metadata": {},
   "source": [
    "### 2．矩阵的减法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "dbe3f5e3-8131-4863-b13e-950299e4c4ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.5030,  0.0206,  0.5811,  0.0691],\n",
      "        [-0.0895,  0.0398, -0.0511, -0.4192],\n",
      "        [ 0.4032,  0.0501, -0.0117,  0.2174]])\n"
     ]
    }
   ],
   "source": [
    "print(a-b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "3db79402-1109-4816-9026-4bb4274bd602",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.5030,  0.0206,  0.5811,  0.0691],\n",
      "        [-0.0895,  0.0398, -0.0511, -0.4192],\n",
      "        [ 0.4032,  0.0501, -0.0117,  0.2174]])\n"
     ]
    }
   ],
   "source": [
    "print(torch.sub(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "29991cc8-6741-47bc-880d-15ecd4698a4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.5030,  0.0206,  0.5811,  0.0691],\n",
      "        [-0.0895,  0.0398, -0.0511, -0.4192],\n",
      "        [ 0.4032,  0.0501, -0.0117,  0.2174]])\n"
     ]
    }
   ],
   "source": [
    "c=torch.Tensor(3,4)\n",
    "print(torch.sub(a,b,out=c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a7d33786-562f-47bb-95a4-6fedaa7a9b5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.5030, -0.0206, -0.5811, -0.0691],\n",
      "        [ 0.0895, -0.0398,  0.0511,  0.4192],\n",
      "        [-0.4032, -0.0501,  0.0117, -0.2174]])\n"
     ]
    }
   ],
   "source": [
    "print(b.sub(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcf6cb58-bedd-46c0-812e-5c325226af3c",
   "metadata": {},
   "source": [
    "### 3．矩阵的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e7ebb8c5-29b6-429f-82d3-995103f644f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3.9443e-01, 3.9089e-04, 4.7075e-02, 6.0872e-01],\n",
      "        [1.4260e-01, 6.2058e-04, 1.5143e-03, 2.4423e-01],\n",
      "        [3.5202e-01, 7.4352e-04, 4.3523e-03, 7.1936e-01]])\n"
     ]
    }
   ],
   "source": [
    "print(a*b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "80e04ac0-f1b0-41e6-8be1-e88f72d64131",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3.9443e-01, 3.9089e-04, 4.7075e-02, 6.0872e-01],\n",
      "        [1.4260e-01, 6.2058e-04, 1.5143e-03, 2.4423e-01],\n",
      "        [3.5202e-01, 7.4352e-04, 4.3523e-03, 7.1936e-01]])\n"
     ]
    }
   ],
   "source": [
    "print(torch.mul(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "cc7f1389-556f-4809-85fd-fc46cce38351",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3.9443e-01, 3.9089e-04, 4.7075e-02, 6.0872e-01],\n",
      "        [1.4260e-01, 6.2058e-04, 1.5143e-03, 2.4423e-01],\n",
      "        [3.5202e-01, 7.4352e-04, 4.3523e-03, 7.1936e-01]])\n"
     ]
    }
   ],
   "source": [
    "c=torch.Tensor(3,4)\n",
    "print(torch.mul(a,b,out=c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c5043102-b327-4152-8ae2-05cda95d0ef1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3.9443e-01, 3.9089e-04, 4.7075e-02, 6.0872e-01],\n",
      "        [1.4260e-01, 6.2058e-04, 1.5143e-03, 2.4423e-01],\n",
      "        [3.5202e-01, 7.4352e-04, 4.3523e-03, 7.1936e-01]])\n"
     ]
    }
   ],
   "source": [
    "print(b.mul(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec8e22a6-9afb-468f-ab1f-c17124ab166b",
   "metadata": {},
   "source": [
    "### 4．矩阵的除法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b1789cd5-20da-40f8-87d0-99ce29aaa41e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2.1834, 2.7213, 9.0624, 1.0926],\n",
      "        [0.7894, 4.3204, 0.2915, 0.4384],\n",
      "        [1.9486, 5.1763, 0.8379, 1.2912]])\n"
     ]
    }
   ],
   "source": [
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "11989451-4f9f-44d5-a2eb-f84ed5ace7e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2.1834, 2.7213, 9.0624, 1.0926],\n",
      "        [0.7894, 4.3204, 0.2915, 0.4384],\n",
      "        [1.9486, 5.1763, 0.8379, 1.2912]])\n"
     ]
    }
   ],
   "source": [
    "print(torch.div(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "20cda54b-e8bb-4b87-8a23-c09700a06881",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2.1834, 2.7213, 9.0624, 1.0926],\n",
      "        [0.7894, 4.3204, 0.2915, 0.4384],\n",
      "        [1.9486, 5.1763, 0.8379, 1.2912]])\n"
     ]
    }
   ],
   "source": [
    "c=torch.Tensor(3,4)\n",
    "print(torch.div(a,b,out=c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b6fe120c-791f-4ac7-a0c7-e66beef02c62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2.1834, 2.7213, 9.0624, 1.0926],\n",
      "        [0.7894, 4.3204, 0.2915, 0.4384],\n",
      "        [1.9486, 5.1763, 0.8379, 1.2912]])\n"
     ]
    }
   ],
   "source": [
    "print(a.div(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7bd1ae4-7ef0-4350-9cdc-66b054bb0c3a",
   "metadata": {},
   "source": [
    "### 5．矩阵的幂运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "1c629e78-4642-43a3-be52-7ccd57900378",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[8.6119e-01, 1.0637e-03, 4.2662e-01, 6.6509e-01],\n",
      "        [1.1257e-01, 2.6811e-03, 4.4146e-04, 1.0707e-01],\n",
      "        [6.8594e-01, 3.8487e-03, 3.6466e-03, 9.2884e-01]])\n"
     ]
    }
   ],
   "source": [
    "print(a.pow(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9f9b712c-0405-44da-9ee0-a52f967b3194",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[8.6119e-01, 1.0637e-03, 4.2662e-01, 6.6509e-01],\n",
      "        [1.1257e-01, 2.6811e-03, 4.4146e-04, 1.0707e-01],\n",
      "        [6.8594e-01, 3.8487e-03, 3.6466e-03, 9.2884e-01]])\n"
     ]
    }
   ],
   "source": [
    "print(a**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5380aef-0b2f-4dab-b04b-c992a9508840",
   "metadata": {},
   "source": [
    "### 6．矩阵的平方根"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c235ed1e-ab22-4b4b-ad14-5b3d8afec528",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.9633, 0.1806, 0.8082, 0.9031],\n",
      "        [0.5792, 0.2276, 0.1450, 0.5720],\n",
      "        [0.9101, 0.2491, 0.2457, 0.9817]])\n"
     ]
    }
   ],
   "source": [
    "print(a.sqrt())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "2828af28-2b80-4f8d-90b6-79e1e23d5f1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.0381, 5.5372, 1.2373, 1.1073],\n",
      "        [1.7264, 4.3946, 6.8989, 1.7482],\n",
      "        [1.0988, 4.0149, 4.0694, 1.0186]])\n"
     ]
    }
   ],
   "source": [
    "print(a.rsqrt())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e02f56f-e1f1-43c8-8e37-8b2cada7102a",
   "metadata": {},
   "source": [
    "### 7．矩阵的对数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "d303fbf7-99e2-43c9-91a4-b40ac1b7ac5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.1078, -4.9383, -0.6145, -0.2942],\n",
      "        [-1.5755, -4.2715, -5.5727, -1.6117],\n",
      "        [-0.2719, -4.0107, -4.0496, -0.0532]])\n"
     ]
    }
   ],
   "source": [
    "print(torch.log2(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "625e3427-d553-49f2-b656-56d58782b254",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.0325, -1.4866, -0.1850, -0.0886],\n",
      "        [-0.4743, -1.2858, -1.6776, -0.4852],\n",
      "        [-0.0819, -1.2073, -1.2191, -0.0160]])\n"
     ]
    }
   ],
   "source": [
    "print(torch.log10(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "702270d4-490f-4a8d-a44a-51dbbdd3cc91",
   "metadata": {},
   "source": [
    "### 8．其他主要运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "1a8c9e2e-2044-4872-8bb3-d929996745a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(3.)\n"
     ]
    }
   ],
   "source": [
    "a=torch.tensor(3.1415926)\n",
    "print(a.floor())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "92526548-44d6-476a-be48-740f61f2e806",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(4.)\n"
     ]
    }
   ],
   "source": [
    "print(a.ceil())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "794e8302-ac59-4c93-8721-9d1e20d9ffaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(3.)\n"
     ]
    }
   ],
   "source": [
    "print(a.round())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "0de84354-4739-4142-8ce5-f77e358d8a1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(3.)\n"
     ]
    }
   ],
   "source": [
    "print(a.trunc())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "17937e47-650c-4406-912f-31f5ebe57606",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.1416)\n"
     ]
    }
   ],
   "source": [
    "print(a.frac())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd0aab1d-69e8-4e30-b1a0-dca8e797226c",
   "metadata": {},
   "source": [
    "## 2.5　动手练习：拟合余弦函数曲线"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d7f5bda-c046-4454-b93a-747ea1913075",
   "metadata": {},
   "source": [
    "### 1.导入相关第三方库，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "20a1d646-49ec-495f-9dca-58391410e263",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.utils.data import DataLoader\n",
    "from torch.utils.data import TensorDataset\n",
    "import numpy as np\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "matplotlib.rcParams[\"font.sans-serif\"]=[\"SimHei\"]\n",
    "matplotlib.rcParams['axes.unicode_minus']=False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af18b7b3-9a33-4b77-9ed0-24a52c74f77b",
   "metadata": {},
   "source": [
    "### 2.准备拟合数据，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "5208e782-b1ca-402e-84de-4cd224d61fa6",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=np.linspace(-2*np.pi,2*np.pi,400)\n",
    "y=np.cos(x)\n",
    "X=np.expand_dims(x,axis=1)\n",
    "Y=y.reshape(400,-1)\n",
    "dataset=TensorDataset(torch.tensor(X,dtype=torch.float),torch.tensor(Y,dtype=torch.float))\n",
    "dataloader=DataLoader(dataset,batch_size=10,shuffle=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc4ead35-2fc3-4e37-bdb7-2314fa769223",
   "metadata": {},
   "source": [
    "### 3.设置神经网络，这里就用一个简单的线性结构，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "123bc8fd-df13-4116-95fb-a91d8ebb1a73",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Net,self).__init__()\n",
    "        self.net=nn.Sequential(\n",
    "            nn.Linear(in_features=1,out_features=10),nn.ReLU(),\n",
    "            nn.Linear(10,100),nn.ReLU(),\n",
    "            nn.Linear(100,10),nn.ReLU(),\n",
    "            nn.Linear(10,1)\n",
    "        )\n",
    "    def forward(self,input:torch.FloatTensor):\n",
    "        return self.net(input)\n",
    "net=Net()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8238f2c0-c482-4421-b4c0-68c27992f50b",
   "metadata": {},
   "source": [
    "### 4.设置优化器和损失函数，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "74f33f9f-1279-4ff7-8676-dab6122cd7ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "optim=torch.optim.Adam(Net.parameters(net),lr=0.001)\n",
    "Loss=nn.MSELoss()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07429922-3b4d-4ac9-b923-b30a1e290ca3",
   "metadata": {},
   "source": [
    "### 5.开始训练模型并进行预测，训练100次，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "0d65778c-4013-4bae-b6fa-29919b4ab544",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练步骤：10,模型损失：0.001291533699259162\n",
      "训练步骤：20,模型损失：0.012467378750443459\n",
      "训练步骤：30,模型损失：0.01596060022711754\n",
      "训练步骤：40,模型损失：0.0030809089075773954\n",
      "训练步骤：50,模型损失：0.006555788218975067\n",
      "训练步骤：60,模型损失：0.0018396865343675017\n",
      "训练步骤：70,模型损失：0.0011598763521760702\n",
      "训练步骤：80,模型损失：0.002187428530305624\n",
      "训练步骤：90,模型损失：0.004019598942250013\n",
      "训练步骤：100,模型损失：0.004570430610328913\n"
     ]
    }
   ],
   "source": [
    "for epoch in range(100):\n",
    "    loss=None\n",
    "    for batch_x,batch_y in dataloader:\n",
    "        y_predict=net(batch_x)\n",
    "        loss=Loss(y_predict,batch_y)\n",
    "        optim.zero_grad()\n",
    "        loss.backward()\n",
    "        optim.step()\n",
    "    if (epoch+1)%10==0:\n",
    "        print(\"训练步骤：{0},模型损失：{1}\".format(epoch+1,loss.item()))\n",
    "predict=net(torch.tensor(X,dtype=torch.float))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e915c3b8-b29f-4f7f-ac5d-cad73f748f36",
   "metadata": {},
   "source": [
    "### 6.绘制预测值和真实值之间的折线图，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49e390a5-d2f4-48df-a257-d7936840028e",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize(12,7),dpi=160)\n",
    "plt.plot(x,y,label=\"实际值\",marker=\"X\")\n",
    "plt.plot(x,predict.detach().numpy(),label=\"预测值\",marker=\"o\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
